//Função para limitar o input de characters dinamicamente no onKeyPress. Recebe um event e um regex para delimitação.
function inputLimiter(event, keyRE) {
    if ( ( typeof(event.keyCode) != 'undefined' && event.keyCode > 0 && String.fromCharCode(event.keyCode).search(keyRE) != (-1) ) || 
        ( typeof(event.charCode) != 'undefined' && event.charCode > 0 && String.fromCharCode(event.charCode).search(keyRE) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode != event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13|45|46|35|36|37|39)$/) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode == event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13)$/) != (-1) ) ) 
        {
        return true;
        
    } else {
        return false;
    }
}

// Função para mostrar o dialog somente se não houver erros
function showDialog(xhr, status, args, dialogWidgetVar) {
    var b = (args.eventProcessedOK == undefined || args.eventProcessedOK == true);
    if(!args.validationFailed && b){
        dialogWidgetVar.show();
    }
}

// Função para fechar o dialog somente se não houver erros
function hideDialog(xhr, status, args, dialogWidgetVar) {
    var b = (args.eventProcessedOK == undefined || args.eventProcessedOK == true);
    if(!args.validationFailed && b){
        dialogWidgetVar.hide();
    }
}

//Funções para limitar o tipo de arquivo no fileupload dialog
$(function(){
    $("#fileUploadId input[type=file]").attr("accept", "application/pdf")
}) 

$(function(){
    $("body").ajaxComplete(function(){
        $("#fileUploadId input[type=file]").attr("accept", "application/pdf")
    })
}) 

//Prevent enter key-press submit
$(function(){
    $("body").keydown(function(e){
        var evt = (e) ? e : ((event) ? event : null);
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if ((evt.keyCode == 13) && ((node.type=="text") ||(node.type=="textarea")) && !$(node).hasClass('ui-column-filter') ) {
            e.preventDefault();
        }
    });
})
